rm(list = ls())
library(here)
library(data.table)
library(qs)
library(lfe)

library(tidyverse)
library(estimatr)
library(texreg)
library(ggthemes)
library(arsenal)

# Load the functions
source(here("code", "functions.R"))

# Load the data
vox <- qread(here("data", "vox.qs")); setDT(vox)

df <- vox %>% filter(BR == 1 & travelMUNmin <= 30)

df <- df %>% 
  mutate(treated = as.numeric(travelMUNmin <= 15),
         treated = recode(treated, `1` = "Treated (0-15 min)", `0` = "Control (15-30 min)"),
         treated = factor(treated))


df <- df %>% 
  mutate(income = case_when(income == 1 ~ "$<$ 3000 CHF",
                            income == 2 ~ "3000--5000 CHF",
                            income == 3 ~ "5000--7000 CHF",
                            income == 4 ~ "7000--9000 CHF",
                            income == 5 ~ "$>$ 9000 CHF", 
                            TRUE ~ NA_character_),
         income = factor(income, levels = c("$<$ 3000 CHF", 
                                            "3000--5000 CHF",
                                            "5000--7000 CHF",
                                            "7000--9000 CHF",
                                            "$>$ 9000 CHF")))



# 1999 --------------------------------------------------------------------
vox_summary1999 <- tableby(treated ~ income + age + female + education + employed + married, data = df %>% filter(year == 1999), 
                           weights = df %>% filter(year == 1999) %>% pull(gewteil), digits = 1) %>% 
  summary(text = "latex",
          labelTranslations = list(income = "Household Income",
                                   age = "Age (Years)",
                                   female = "Female",
                                   education = "Highest Education Level",
                                   employed = "Currently Employed",
                                   married = "Currently Married"),
          pfootnote = T, width = 5)


capture.output(vox_summary1999, file= here("tables", "summary_stats", "vox_summary1999.tex"))


# 2016 --------------------------------------------------------------------
vox_summary2016 <- tableby(treated ~ income + age + female + education + employed + married, data = df %>% filter(year == 2016), 
                           weights = df %>% filter(year == 2016) %>% pull(gewteil), digits = 1) %>% 
  summary(text = "latex",
          labelTranslations = list(income = "Household Income",
                                   age = "Age (Years)",
                                   female = "Female",
                                   education = "Highest Education Level",
                                   employed = "Currently Employed",
                                   married = "Currently Married"),
          pfootnote = T, width = 5)


capture.output(vox_summary2016, file= here("tables", "summary_stats", "vox_summary2016.tex"))
